package co.th.bjc.md.helper;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import co.th.bjc.md.common.ConnectDB;
import co.th.bjc.md.model.Expertises;

public class ExpertisesHelper {

	private static Logger _log = Logger.getLogger(ExpertisesHelper.class);
	
	public Expertises findExpertisesByExpertisesID(BigDecimal expertisesCode) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Expertises expertises = null;
		ConnectDB connectDB = null;
		try{
			String sql = "SELECT * FROM expertises WHERE expertises_id = ?";
			_log.info("SQL : " + sql);
			
			connectDB = new ConnectDB();
			conn = connectDB.getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setBigDecimal(1, expertisesCode);
			
			rs = stmt.executeQuery();
			if (rs.next()){
				expertises = this.setExpertises(rs);
			}
		}catch(Exception ex){
			_log.error("findExpertisesByExpertisesID --> " + ex.toString());
			ex.printStackTrace();
		}finally{
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			connectDB.close();
		}
		return expertises;
	}
	
	private Expertises setExpertises(ResultSet rs) throws Exception{
		Expertises exp = new Expertises();
		exp.setExpertisesId(rs.getBigDecimal("expertises_id"));
		exp.setExpertisesName(rs.getString("expertises_name"));
		exp.setDepartment(null);
		exp.setCreateBy(rs.getString("create_by"));
		exp.setCreateDate(rs.getDate("create_date"));
		exp.setUpdateBy(rs.getString("update_by"));
		exp.setUpdateDate(rs.getDate("update_date"));
		return exp;
	}
}
